﻿using CC.Common;
using Common;
using RsaRegister;
using RsaRegister.Model;
using SoftRegManager.BLL;
using SoftRegManager.Forms;
using SoftRegManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SoftRegManager
{
    public partial class Form1 : Form
    {

        GreatBLL mBLL;
        SettingItems mSettings;
        string mSettingFile = "setting.ini";
        SoftInfo mAppInfo;
        BindingList<RegInfo> mRegInfoes;
        public Form1()
        {
            InitializeComponent();
        }

       

        private void Form1_Load(object sender, EventArgs e)
        {
            dgvData.AutoGenerateColumns = false;
            if (File.Exists(mSettingFile))
            {
                if(!LoadSettings(mSettingFile))
                {
                    toolStrip1.Enabled = false;
                    MessageBox.Show("装载配置文件失败，请检查配置","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    mnuSetting_Click(sender, e);
                }
                else
                {
                    mBLL = new GreatBLL(mSettings.ConnString, mSettings.SqlType);
                    var oftinfoes = mBLL.GetSoftwareInfoes().ToList();
                    foreach (var soft in oftinfoes)
                    {
                        cbApps.Items.Add(soft.SoftName);
                    }
                }
            }
            else
            {
                toolStrip1.Enabled = false;
                MessageBox.Show("缺少配置文件，请先配置系统！","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                mnuSetting_Click(sender, e);
            }
        }
        private bool LoadSettings(string settingFile)
        {
            try
            {
                mSettings = new SettingItems();
                MyConfiguration cfg = new MyConfiguration(settingFile,true);
                cfg.ReadConfigurationsToObject(mSettings, settingFile);
                if (mSettings != null)
                {
                    mBLL = new GreatBLL(mSettings.ConnString, mSettings.SqlType);
                }

                return mBLL.DbReady;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"装入设置错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return false;
            }
           
        }
        private void mnuSetting_Click(object sender, EventArgs e)
        {
            FrmSetting frm = new FrmSetting();
            if( frm.ShowDialog()== DialogResult.OK)
            {
               toolStrip1.Enabled= LoadSettings(mSettingFile);
            }
        }

        private void mnuSoftwareManager_Click(object sender, EventArgs e)
        {
            FrmSoftwareInfoes frm = new FrmSoftwareInfoes(mBLL);
            if(frm.ShowDialog()== DialogResult.OK)
            {
                var softes = mBLL.GetSoftwareInfoes().ToList();
                cbApps.Items.Clear();
                foreach (var soft in softes)
                {
                    cbApps.Items.Add(soft.SoftName);
                }
            }
        }

        private void cbApps_SelectedIndexChanged(object sender, EventArgs e)
        {
            mAppInfo = mBLL.GetSoftwareInfoes().First(s => s.SoftName == cbApps.Text);
            IList<RegInfo> regInfoes = mBLL.GetSoftRegInfoes().Where(s => s.SoftName == cbApps.SelectedItem.ToString()).ToList();
            mRegInfoes = new BindingList<RegInfo>(regInfoes);
            dgvData.DataSource = mRegInfoes;
        }

        private void addLicenseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(mAppInfo==null)
            {
                MessageBox.Show("未选定APP。请先选定APP","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);

                return;
            }
            FrmLicenseEdit frm = new FrmLicenseEdit(mAppInfo, mBLL);
            if(frm.ShowDialog()== DialogResult.OK)
            {
                mRegInfoes.Add(frm.RegInfo);
            }
        }

        private void editLicenseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvData.CurrentRow.Index < 0) return;
            var reginfo = mRegInfoes[dgvData.CurrentRow.Index];
            var softinfo = mBLL.GetSoftwareInfo(cbApps.Text);
            if(softinfo!=null)
            {
                FrmLicenseEdit frm = new FrmLicenseEdit(softinfo, mBLL, reginfo);
                if(frm.ShowDialog()== DialogResult.OK)
                {
                    mRegInfoes[dgvData.CurrentRow.Index] = frm.RegInfo;
                }
            }
           
        }

        private void dgvData_DoubleClick(object sender, EventArgs e)
        {
           
            editLicenseToolStripMenuItem_Click(sender, e);
        }
    }
}
